package com.zyl.controller;

import com.zyl.Mapper.CategoryMapper;
import com.zyl.enums.EmailEnum;
import com.zyl.enums.UserEnum;
import com.zyl.pojo.FriendLink;
import com.zyl.pojo.Update;
import com.zyl.pojo.User;
import com.zyl.service.CategoryService;
import com.zyl.service.FriendService;
import com.zyl.service.UpdateService;
import com.zyl.service.UserService;
import com.zyl.util.ResultVoUtil;
import com.zyl.vo.CategoryVo;
import com.zyl.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Zyl
 * @Param:
 * @return:
 * @Description:
 */
@RestController
public class index {

    @Autowired
    CategoryService categoryService;
    @Autowired
    UpdateService updateService;
    @Autowired
    FriendService friendService;
    @Autowired
    UserService userService;
    @GetMapping("/category")
    public ResultVo findAllCategory(){
        List<CategoryVo> allCategory = categoryService.findAllCategory();
        return ResultVoUtil.success(allCategory);
    }
    @GetMapping("/findCategory/{articleName}")
    public ResultVo findCategory(@PathVariable("articleName") String articleName){
        List<CategoryVo> categoryById = categoryService.findCategoryByArticleName(articleName);
        return ResultVoUtil.success(categoryById);
    }
    @GetMapping("/update")
    public ResultVo updateAll(){
        List<Update> all = updateService.findAll();
        return ResultVoUtil.success(all);
    }
    @GetMapping("/friend")
    public ResultVo findFriend(){
        List<FriendLink> all = friendService.findAll();
        return ResultVoUtil.success(all);
    }
    @PostMapping("/register")
    public ResultVo register(@RequestBody User user){
        Integer username1 = userService.findUsername(user.getUsername());
        Integer email = userService.findEmail(user.getEmail());
        if (username1!=0){
            return ResultVoUtil.fail(UserEnum.USEREXIT);
        }
        if (email!=0){
            return ResultVoUtil.fail(EmailEnum.EMAILEXIT);
        }
        userService.addUser(user);
        return ResultVoUtil.success(null);
    }
    @GetMapping("/username/{username}")
    public ResultVo findUsername(String username){
        Integer username1 = userService.findUsername(username);
        System.out.println(username1);
        if (username1==1){
            return ResultVoUtil.fail(UserEnum.USEREXIT);
        }else {
            return ResultVoUtil.success(null);
        }
    }
}
